<?php
$topic = ProjectSelectedTopics::model()->find("project_id=1 and project_topic_id=7737");
$topic->canBeObjective();
?>

<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::errorSummary($model); ?>

<?php

$cs = Yii::app()->clientScript;

echo CHtml::activeHiddenField($model, 'project_id') . "\n";
echo CHtml::activeHiddenField($model, 'id') . "\n";
$form_name = 'ProjectTopic';
?>

<h2><?php echo Yii::t('project', 'Identification'); ?></h2>
<div class="yiiForm" id="identification">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
	<td width="50%" valign="top">
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'First Name') . "&nbsp;:&nbsp;", '');
echo $this->dashed($model->project->owner->gender . " " . $model->project->owner->first_name . " " . $model->project->owner->last_name . "&nbsp;");
?>
		</div>
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'Birth Date') . "&nbsp;:&nbsp;", '');
if ($model->project->owner->birth_date != null && $model->project->owner->birth_date != "")
	echo $this->dashed($this->convert_date($model->project->owner->birth_date, 'yyyy-MM-dd', 'dd MMMM yyyy'));
else
	echo $this->dashed("&nbsp;");
?>
		</div>
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'Referent') . "&nbsp;:&nbsp;", '');
echo $this->dashed($model->project->owner->referent . "&nbsp;");
?>
		</div>
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'Service') . "&nbsp;:&nbsp;", '');
echo $this->dashed($model->project->owner->service->label . "&nbsp;");
?>
		</div>
	</td>
	<td width="50%" valign="top">
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'Address1') . "&nbsp;:&nbsp;", '');
$addresses = array ();
if (isset ($model->project->owner->address1) && $model->project->owner->address1 != null & $model->project->owner->address1 != "")
	$addresses[] = $model->project->owner->address1;
if (isset ($model->project->owner->address2) && $model->project->owner->address2 != null & $model->project->owner->address2 != "")
	$addresses[] = $model->project->owner->address2;
if (isset ($model->project->owner->address3) && $model->project->owner->address3 != null & $model->project->owner->address3 != "")
	$addresses[] = $model->project->owner->address3;
$addresses[] = $model->project->owner->city->postal_code . " " . $model->project->owner->city->city_name;
$addresses[] = $model->project->owner->city->country->country_name;

echo $this->dashed($addresses);
?>
		</div>
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'Email') . "&nbsp;:&nbsp;", '');
echo $this->dashed($model->project->owner->email . "&nbsp;");
?>
		</div>
	</td>
</tr>
<tr>
	<td>
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'Activity') . "&nbsp;:&nbsp;", '');
echo $this->dashed($model->project->activity . "&nbsp;");
?>
		</div>
	</td>
	<td>
		<div class="simple">
		<?php


echo CHtml::label(Yii::t('database', 'Route') . "&nbsp;:&nbsp;", '');
echo $this->dashed($model->project->route->label . "&nbsp;");
?>
		</div>
	</td>
</tr>
</table>
</div><!-- yiiForm -->

<h2><?php echo Yii::t('project', 'Previous objectives'); ?></h2>
<div class="yiiForm" id="objectives">
<ul id="objectives">
<?php
foreach ($model->objectives() as $topic) {
	echo "<li><a href='#'>".$topic->topic->label."</a></li>";
	$this->widget('adviser.widgets.CommentLayer', array (
		'topic_id' => $topic->id,
		'columns' => 60,
		'rows' => 5,
		'prefix' => '',
		'adviser_validation' => $topic->adviser_validation,
		'adviser_topic_validation' => $topic->adviser_validation_topic,
		'owner_validation' => $topic->owner_validation,
		'owner_topic_validation' => $topic->owner_validation_topic,
		'comment' => $topic->validation_comments,
		'form_name' => $model
	));
}
?>
</ul>
<table cellspacing="0" cellpadding="0" width="99%">
<tr>
	<td>
		<?php


$topics = array ();
foreach ($model->objectives() as $topic) {
	$topics[$topic->id] = $topic->topic->label;
}
echo CHtml::listBox('objectives', '', $topics, array (
	'size' => 10
));
?>
	</td>
	<td>
		<?php


$this->widget('adviser.widgets.CommentLayer', array (
	'topic_id' => 1,
	'style' => '',
	'columns' => 60,
	'rows' => 9,
	'prefix' => 'o_',
	'area_style' => 'width: 100%; height:100%;',
	'form_name' => $form_name
));
?>
	</td>
</tr>
</table>
</div><!-- yiiForm -->

<h2><?php echo Yii::t('project', 'Others validated items'); ?></h2>
<div class="yiiForm" id="project_topic">
	<div id="project_topic_tree">
	<?php
	
	
	displaySteps($this, $form_name, $model->steps(), false);
	?>
	</div><!-- project_topic_tree -->
</div><!-- yiiForm -->

<h2><?php echo Yii::t('project', 'Objectives for next meeting'); ?></h2>
<div class="yiiForm" id="next_objectives">
	<div id="next_objectives_tree">
	<?php
	displaySteps($this, $form_name, $model->steps(), true);
	?>
	</div><!-- yiiForm -->
</div><!-- yiiForm -->

<div class="action">
<?php echo CHtml::submitButton(Yii::t('views', 'Create')); ?>
</div>

<?php

echo CHtml::endForm();

function displaySteps($p, $model, $steps, $objectivesOnly = false) {
	if (is_array($steps) && count($steps) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($steps as $step) {
			if (!$objectivesOnly || $step->canBeObjective()) {
				echo "<li id='".($objectivesOnly ? "o_" : "")."node_" . $step->mnemonic . "' class='open' style='width:100%;'>\n";
				echo "\t<a href='#'>" . $step->label . "</a>";
				displayTopics($p, $model, $step->topics, $objectivesOnly);
				echo "</li>";
			}
		}
		echo "</ul>";
	}
}

function displayTopics($p, $model, $topics, $objectivesOnly) {
	$image = "";

	if (is_array($topics) && count($topics) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($topics as $topic) {
			if ($objectivesOnly) {
				$image = "<a href='#'><ins>&nbsp;</ins></a>";

			} else {
				$image = CHtml::image('images/arrow_down_blue.png', 'Show/Hide validation comments', array (
					'onclick' => 'javascript:jQuery.toggle_comment("' . $topic->project_topic_id . '")',
					'id' => 'img_comment_' . $topic->project_topic_id
				));
			}
			if (!$objectivesOnly || $topic->canBeObjective()) {
				echo "<li id='".($objectivesOnly ? "o_" : "")."node_" . $topic->project_topic_id . "'>\n" .
				"\t\n\t$image\n\t" .
				"\n\t<a href='#'>" . $topic->label . "</a>";
				if ($objectivesOnly) {
					$p->widget('adviser.widgets.ObjectiveLayer', array (
						'topic_id' => $topic->project_topic_id,
						'columns' => 60,
						'rows' => 5,
						'prefix' => '',
						'objective' => $topic->topic->objective,
						'objective_topic' => $topic->topic->objective_topic,
						'objective_needs' => $topic->topic->objective_needs,
						'objective_contacts' => $topic->topic->objective_contacts,
						'form_name' => $model
					));
				} else {
					$p->widget('adviser.widgets.CommentLayer', array (
						'topic_id' => $topic->project_topic_id,
						'columns' => 60,
						'rows' => 5,
						'prefix' => '',
						'adviser_validation' => $topic->topic->adviser_validation,
						'adviser_topic_validation' => $topic->topic->adviser_validation_topic,
						'owner_validation' => $topic->topic->owner_validation,
						'owner_topic_validation' => $topic->topic->owner_validation_topic,
						'comment' => $topic->topic->validation_comments,
						'form_name' => $model
					));
				}
				displayTopics($p, $model, $topic->children, $objectivesOnly);
				echo "</li>";
			}
		}
		echo "</ul>";
	}
}

$this->Widget('application.extensions.jsTree.CjsTree', array (
	'htmlOptions' => array (
		'id' => 'project_topic_tree'
	),
	//'ui' => array (
	//	'theme_name' => 'checkbox'
	//),
	//'options' => array (
	//	'plugins' => array (
	//		'checkbox' => array (
	//			'three_state' => 'true'
	//		)
	//	)
	//),
	'genDiv' => false,
	'callback' => array (
		'onload' => "js:function() { \n" .
		"}"
	)
));

$this->Widget('application.extensions.jsTree.CjsTree', array (
	'htmlOptions' => array (
		'id' => 'next_objectives_tree'
	),
	'ui' => array (
		'theme_name' => 'checkbox'
	),
	'options' => array (
		'plugins' => array (
			'checkbox' => array (
				'three_state' => 'false'
			)
		)
	),
	'genDiv' => false,
	'callback' => array (
		'onload' => "js:function() { \n" .
		"}"
	)
));

$script =<<<JS
$("div[id^='comment_layer_']").hide();
$("div[id^='objective_layer_']").hide();
$("li[id^='node_']").topic_validation();

$("#project_topic_tree").parent().append('<hr class="separation" />');
$("#next_objectives_tree").parent().append('<hr class="separation" />');
JS;
$cs->registerScript('topics_form_save', $script);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/masks.js', CClientScript::POS_HEAD);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/topic_validation.js', CClientScript::POS_HEAD);
?>
